import pygame
import sys
import random
import time

# 初始化pygame
pygame.init()
pygame.font.init()

# 确保中文能正常显示
font = pygame.font.SysFont(['SimHei', 'WenQuanYi Micro Hei', 'Heiti TC'], 48)
small_font = pygame.font.SysFont(['SimHei', 'WenQuanYi Micro Hei', 'Heiti TC'], 24)

# 游戏窗口设置
WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("别踩白块")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 游戏参数
FPS = 60
BLOCK_SIZE = WIDTH // 4  # 每列宽度
BLOCK_SPEED = 5  # 方块下落速度
INITIAL_BLOCKS = 4  # 初始方块行数
SCORE_INCREASE = 1  # 每踩中一个黑块增加的分数


# 方块类
class Block:
    def __init__(self, col, row, is_black=False):
        self.col = col
        self.row = row
        self.x = col * BLOCK_SIZE
        self.y = row * BLOCK_SIZE
        self.is_black = is_black
        self.clicked = False
        self.width = BLOCK_SIZE
        self.height = BLOCK_SIZE

    def update(self):
        self.row += BLOCK_SPEED / BLOCK_SIZE
        self.y = self.row * BLOCK_SIZE

    def draw(self, screen):
        color = BLACK if self.is_black else WHITE
        pygame.draw.rect(screen, color, (self.x, self.y, self.width, self.height))
        pygame.draw.rect(screen, GRAY, (self.x, self.y, self.width, self.height), 1)  # 边框

        # 如果是黑块并且已被点击，显示绿色
        if self.is_black and self.clicked:
            pygame.draw.rect(screen, GREEN, (self.x, self.y, self.width, self.height), 5)

    def check_click(self, pos):
        x, y = pos
        if (self.x <= x < self.x + self.width and
                self.y <= y < self.y + self.height and
                self.is_black and not self.clicked):
            self.clicked = True
            return True
        return False

    def is_out_of_screen(self):
        return self.y > HEIGHT


# 游戏类
class Game:
    def __init__(self):
        self.blocks = []
        self.score = 0
        self.game_over = False
        self.started = False
        self.high_score = 0
        self.setup_game()

    def setup_game(self):
        self.blocks = []
        self.score = 0
        self.game_over = False

        # 创建初始方块
        for row in range(INITIAL_BLOCKS):
            black_col = random.randint(0, 3)
            for col in range(4):
                is_black = (col == black_col)
                block = Block(col, -row, is_black)
                self.blocks.append(block)

    def update(self):
        if not self.started or self.game_over:
            return

        # 更新所有方块位置
        for block in self.blocks:
            block.update()

        # 移除离开屏幕的方块
        self.blocks = [block for block in self.blocks if not block.is_out_of_screen()]

        # 检查是否需要添加新的一行方块
        if self.blocks and self.blocks[0].row > 0:
            black_col = random.randint(0, 3)
            for col in range(4):
                is_black = (col == black_col)
                block = Block(col, -1, is_black)
                self.blocks.insert(0, block)

        # 检查游戏是否结束
        for block in self.blocks:
            if block.row * BLOCK_SIZE > HEIGHT - BLOCK_SIZE and block.is_black and not block.clicked:
                self.game_over = True
                if self.score > self.high_score:
                    self.high_score = self.score

    def draw(self, screen):
        # 绘制背景
        screen.fill(WHITE)

        # 绘制所有方块
        for block in self.blocks:
            block.draw(screen)

        # 绘制分数
        score_text = font.render(f"分数: {self.score}", True, BLACK)
        screen.blit(score_text, (10, 10))

        # 绘制最高分
        high_score_text = small_font.render(f"最高分: {self.high_score}", True, BLACK)
        screen.blit(high_score_text, (10, 60))

        # 游戏未开始时显示开始提示
        if not self.started:
            start_text = font.render("点击开始游戏", True, BLACK)
            screen.blit(start_text, (WIDTH // 2 - start_text.get_width() // 2, HEIGHT // 2 - 50))

        # 游戏结束时显示结束提示
        if self.game_over:
            game_over_text = font.render("游戏结束", True, RED)
            restart_text = small_font.render("点击重新开始", True, BLACK)

            screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - 50))
            screen.blit(restart_text, (WIDTH // 2 - restart_text.get_width() // 2, HEIGHT // 2 + 20))

    def handle_click(self, pos):
        if not self.started:
            self.started = True
            return

        if self.game_over:
            self.setup_game()
            return

        clicked = False
        for block in self.blocks:
            if block.check_click(pos):
                self.score += SCORE_INCREASE
                clicked = True
                break

        # 如果点击了白块，游戏结束
        if not clicked:
            y = pos[1]
            for block in self.blocks:
                if (block.y <= y < block.y + BLOCK_SIZE and
                        not block.is_black):
                    self.game_over = True
                    if self.score > self.high_score:
                        self.high_score = self.score
                    break


# 主函数
def main():
    clock = pygame.time.Clock()
    game = Game()

    running = True
    while running:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:  # 左键点击
                    game.handle_click(event.pos)

        # 更新游戏
        game.update()

        # 绘制游戏
        game.draw(screen)

        # 更新显示
        pygame.display.flip()

    pygame.quit()
    sys.exit()


if __name__ == "__main__":
    main()